package com.aiwhose.aiwhosesystem.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.aiwhose.aiwhosecore.bean.Result;
import com.aiwhose.aiwhosecore.bean.system.Banner;
import com.aiwhose.aiwhosecore.bean.system.SysDictData;
import com.aiwhose.aiwhosesystem.service.BannerService;
import com.aiwhose.aiwhosesystem.service.SysDictDataService;
import com.aiwhose.aiwhosesystem.utils.RedisUtil;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.annotation.Resource;
import lombok.Data;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 轮播图 前端控制器
 * </p>
 *
 * @author 田国刚
 * @since 2024-01-05
 */
@RestController
@SaCheckLogin
@CrossOrigin
@RequestMapping("/system")
public class SysDataController {
    @Resource
    RedisUtil redisUtil;
    @Resource
    SysDictDataService sysDictDataService;
    @Resource
    BannerService bannerService;


    //查询
    @SaIgnore
    @GetMapping("/getSysDictDataByTypeAndKey")
    public Result getSysDictDataByTypeAndKey(@Parameter SysDictData sysDictData) {
        Object typeObj = redisUtil.get("system:sysDictData:" + sysDictData.getDictType() + ":" + sysDictData.getDictKey());
        if (ObjectUtil.isNotEmpty(typeObj)) {
            return Result.ok(JSONUtil.parse(typeObj));
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper
                .eq(SysDictData::getDictType, sysDictData.getDictType())
                .eq(SysDictData::getDictKey, sysDictData.getDictKey())
                .eq(SysDictData::getStatus, 0);
        SysDictData sysDictDataRes = sysDictDataService.getOne(queryWrapper);
        if (ObjectUtil.isNotEmpty(sysDictDataRes)) {
            redisUtil.set("system:sysDictData:" + sysDictData.getDictType() + ":" + sysDictData.getDictKey(), JSONUtil.toJsonStr(sysDictDataRes), 60 * 60 * 24 * 30);
        }
        return Result.ok(sysDictDataRes);
    }

    //查询
    @SaIgnore
    @GetMapping("/getSysDictDataListByType")
    public Result getSysDictDataListByType(@Parameter SysDictData sysDictData) {
        Object typeObj = redisUtil.get("system:data:sysDictDataList" + sysDictData.getDictType());
        if (ObjectUtil.isNotEmpty(typeObj)) {
            return Result.ok(JSONUtil.parseArray(typeObj));
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper
                .eq(SysDictData::getDictType, sysDictData.getDictType())
                .eq(SysDictData::getStatus, 0);
        List<SysDictData> sysDictDataList = sysDictDataService.list(queryWrapper);
        if (!sysDictDataList.isEmpty()) {

            redisUtil.set("system:sysDictDataList" + sysDictData.getDictType(), sysDictDataList, 60 * 60 * 24 * 30);
        }
        return Result.ok(sysDictDataList);
    }

    //查询
    @SaIgnore
    @GetMapping("/getSysBannersByType")
    public Result getSysBannersByType(@Parameter Banner banner) {
        Object typeObj = redisUtil.get("system:sysBanner:" + banner.getType());
        if (ObjectUtil.isNotEmpty(typeObj)) {
            return Result.ok(JSONUtil.parseArray(typeObj));
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper
                .eq(Banner::getType, banner.getType())
                .eq(Banner::getStatus, 0)
                .le(Banner::getStartTime, LocalDateTime.now())
                .ge(Banner::getEndTime, LocalDateTime.now());
        List<Banner> bannerList = bannerService.list(queryWrapper);
        if (bannerList.size() > 0) {
            redisUtil.set("system:sysBanner:" + banner.getType(), JSONUtil.toJsonStr(bannerList), 60 * 60 * 24 * 30);
        }
        return Result.ok(bannerList);
    }
}
